package com.donger.upms.biz.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.upms.api.entity.SysOauthClientDetails;
import com.donger.upms.biz.service.SysOauthClientDetailsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 终端信息表(SysOauthClientDetails)表控制层
 *
 * @author aeizzz
 * @since 2019-04-25 13:55:34
 */
@RestController
@RequestMapping("/sys/client")
@AllArgsConstructor
@Slf4j
public class SysOauthClientDetailsController extends BaseController {
    /**
     * 服务对象
     */
    private final SysOauthClientDetailsService sysOauthClientDetailsService;

    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param entity 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    public Result selectAll(Page<SysOauthClientDetails> page, SysOauthClientDetails entity) {
        return Res.ok(sysOauthClientDetailsService.page(page, new LambdaQueryWrapper<SysOauthClientDetails>()
                .like(StrUtil.isNotBlank(entity.getClientId()), SysOauthClientDetails::getClientId, entity.getClientId())
                .like(StrUtil.isNotBlank(entity.getClientSecret()), SysOauthClientDetails::getClientSecret, entity.getClientSecret())
        ));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/info/{id}")
    public Result selectOne(@PathVariable("id") String id) {
        return Res.ok(sysOauthClientDetailsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param entity 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    public Result insert(@Valid @RequestBody SysOauthClientDetails entity) {
        sysOauthClientDetailsService.save(entity);
        return Res.ok();
    }

    /**
     * 修改数据
     *
     * @param entity 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result update(@Valid @RequestBody SysOauthClientDetails entity) {
        sysOauthClientDetailsService.updateById(entity);
        return Res.ok();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        sysOauthClientDetailsService.removeById(id);
        return Res.ok("删除成功");
    }
}